#define versionInfo "zxx 20131029 v36"

#include "commons.h"
#include "tasks.h"
#include "zxLib.h"
#include "ldLib.h"
#include "HZLib.h"
#include "cq.h"



_FOSC(CSW_FSCM_OFF & XT_PLL4);
_FWDT(WDT_OFF);
_FBORPOR(PBOR_ON & BORV20 & PWRT_64 & MCLR_EN);

int main(void) {
    ADPCFG = 0xFFFF;
    SetupPorts();
    Init_PWM();
    InitTMR();
    Init_LCD();
    InitUART2();


    unsigned char StartText[] = {0x00, 0x01, 0x02, 0x03};
    Puts_UART2((unsigned char*) &StartText[0], sizeof (StartText));
    Flags.pun = 0;

    splash("Happy Train Mice", versionInfo);


    while (1) {
        unsigned int n = getFuncNumber(4, "Main Function ?");
        srand(timerCounterI);
        switch (n) {
            case 1111:
                n = getFuncNumber(1, " 2  5  10 30 600");
                feedWater(n);
                break;
            case 1112:
                test_odorA();
                break;
            case 1113:
                test_odorB();
                break;
            case 1114:
                test_odorC();
                break;
            case 1115:
                test_odorD();
                break;
            case 1121:
                test_Laser();
                break;
            case 1122:
                test_lick();
                break;
            case 1123:
                test_BNC();
                break;
            case 1124:
                n = getFuncNumber(1, "IntOdr 1=5s 2=2s");
                if (n == 2) {
                    AB_BA_Go(2, 5, 1.0, 0.5);
                } else {
                    AB_BA_Go(5, 10, 1.0, 0.5);
                }
                break;
            case 1135:
                AA_BB_Go(5, 10, 1.0, 0.5);
                break;
           
                ///////Ld functions
            case 1511:
                LD_shaping_AB_BA_Go(3.0, 5, 1.0, 1.0, 10, 5); // for normal training
                break;
            case 1512:
                LD_shaping_AB_BA_Go(5.0, 10, 0.5, 1.0, 10, 5); // for normal training
                break;
            case 1513:
                LD_shaping_AB_BA_Go(3.0, 5, 1.0, 0.0, 10, 5); // for dual task
                break;
            case 1514:
                LD_shaping_AB_BA_Go(5.0, 10, 1.0, 0.0, 10, 5); // for dual task
                break;
            case 1515:
                LD_auto_AB_BA_Go_AA_BB_Nogo_Ran_new(5.0, 10, 20, 0.5, 1.0, 6, 20); // for normal training
                break;
            case 1521:
                LD_auto_AB_BA_Go_AA_BB_Nogo_Ran_new(5.0, 10, 20, 1.0, 0.0, 6, 20); // for dual task
                break;
            case 1522:
                CD_Go_Nogo(0.5, 5.0, 20, 1.0, 10, 10, 0.0);
                break;
            case 1523:
                auto_AB_BA_Go_AA_BB_Nogo_Ran2(1.0, 10.0, 8.0, 1.0, 10.0, 40.0, 0.0);
                break;
            case 1524:
                LD_shaping_AB_BA_Go(5.0, 10, 0.5, 1.0, 50, 5); // for normal training(laseron)
                break;
            case 1525:
                LD_auto_AB_BA_Go_AA_BB_Nogo_Ran_new(5.0, 10, 20, 0.5, 1.0, 50, 5); // for normal training(laseron)
                break;
            case 1531:
                FourOdor_DNMS(odor_A, odor_B, odor_C, odor_D, 5, 10, 24);
                break;
            case 1532:
                DNMS_5delays(5, 8, 11, 14, 17, 5 + 5, 8 + 5, 11 + 5, 14 + 5, 17 + 5);
                break;
            case 1533:
                FourOdor_shaping(odor_A, odor_B, odor_C, odor_D, 5, 10, 24);
                break;
             case 1534:
                LD_auto_AB_BA_Go_AA_BB_Nogo_Ran_new_ONOFF(5.0, 10, 20, 0.5, 1.0, 50, 5);  // for normal training(one-on-on-off) 
                break;
                
           case 1535:
                LD_shaping_AB_BA_Go_ONOFF(5.0, 10, 0.5, 1.0, 50, 5); // for normal training(one-on-on-off)
                break;
                /////////////CQ functions//////////////////
            case 2111:
                n = getFuncNumber(1, "BGo1 JGo2 Rand");
                int m = getFuncNumber(1, "NL1 O2 D3 W4 I5");
                int SOneLaserReverseOrnot = getFuncNumber(1, "SOneL1 RevOrnot2");
                CQ_go_nogo_autoSwitch(n, m, SOneLaserReverseOrnot, 20, odor_A, odor_B, 0.5, 1.5, 0.5, 4); //deliver laser during the middle of delay
                break;
            case 2112:
                n = getFuncNumber(1, "Nonmatch1 Match2");
                m = getFuncNumber(1, "NL1 O2 D3 W4 I5");
                int SOneLaserSwitchOrnot = getFuncNumber(1, "SOneL1 SwiOrnot2");
                int OdorPairSelection = getFuncNumber(1, "BJ1 MK2");
                int shapingOrTraining = getFuncNumber(1, "Shape1 Training2");
                CQ_Match_Nonmatch_autoSwitch(n, m, 1,SOneLaserSwitchOrnot, OdorPairSelection, shapingOrTraining, odor_A, 2, 0.0, 1.0, odor_B, 3, 6.0, 7.0, 4, 8.0, 8.5, 18.5, 6, 6, 20, odor_C, odor_D);
                break;
             case 2113:
                n = getFuncNumber(1, "Nonmatch1 Match2");
                m = getFuncNumber(1, "NL1 O2 D3 W4 I5");
                SOneLaserSwitchOrnot = getFuncNumber(1, "SOneL1 SwiOrnot2");
                OdorPairSelection = getFuncNumber(1, "BJ1 MK2");
                shapingOrTraining = getFuncNumber(1, "Shape1 Training2");
                CQ_Match_Nonmatch_autoSwitch(n, m, 1,SOneLaserSwitchOrnot, OdorPairSelection, shapingOrTraining, odor_A, 2, 0.0, 1.0, odor_B, 3, 6.0, 7.0, 4, 8.0, 8.5, 14.5, 6, 6, 20, odor_C, odor_D);
                break;
             case 2114:
                n = getFuncNumber(1, "Nonmatch1 Match2");
                m = getFuncNumber(1, "NL1 O2 D3 W4 I5");
                SOneLaserSwitchOrnot = getFuncNumber(1, "SOneL1 SwiOrnot2");
                OdorPairSelection = getFuncNumber(1, "BJ1 MK2");
                shapingOrTraining = getFuncNumber(1, "Shape1 Training2");
                CQ_Match_Nonmatch_autoSwitch(n, m, 1,SOneLaserSwitchOrnot, OdorPairSelection, shapingOrTraining, odor_A, 2, 0.0, 1.0, odor_B, 3, 4.0, 5.0, 4, 5.0, 5.5, 18.5, 6, 6, 20, odor_C, odor_D);
                break;
             case 2115:
                n = getFuncNumber(1, "Nonmatch1 Match2");
                m = getFuncNumber(1, "NL1 O2 D3 W4 I5");
                SOneLaserSwitchOrnot = getFuncNumber(1, "SOneL1 SwiOrnot2");
                OdorPairSelection = getFuncNumber(1, "BJ1 MK2");
                shapingOrTraining = getFuncNumber(1, "Shape1 Training2");
                CQ_Match_Nonmatch_autoSwitch(n, m, 4,SOneLaserSwitchOrnot, OdorPairSelection, shapingOrTraining, odor_A, 2, 0.0, 1.0, odor_B, 3, 4.0, 5.0, 4, 6.0, 6.5, 11.5, 6, 6, 20, odor_C, odor_D);
                break;
             case 2121:
                n = getFuncNumber(1, "Nonmatch1 Match2");
                m = getFuncNumber(1, "NL1 O2 D3 W4 I5");
                SOneLaserSwitchOrnot = getFuncNumber(1, "SOneL1 SwiOrnot2");
                OdorPairSelection = getFuncNumber(1, "BJ1 MK2");
                shapingOrTraining = getFuncNumber(1, "Shape1 Training2");
                CQ_Match_Nonmatch_autoSwitch(n, m, 2,SOneLaserSwitchOrnot, OdorPairSelection, shapingOrTraining, odor_A, 2, 0.0, 1.0, odor_B, 3, 6.0, 7.0, 4, 9.0, 9.5, 19.5, 6, 6, 20, odor_C, odor_D);
                break;
             case 2122:
                n = getFuncNumber(1, "Nonmatch1 Match2");
                m = getFuncNumber(1, "NL1 O2 D3 W4 I5");
                SOneLaserSwitchOrnot = getFuncNumber(1, "SOneL1 SwiOrnot2");
                OdorPairSelection = getFuncNumber(1, "BJ1 MK2");
                shapingOrTraining = getFuncNumber(1, "Shape1 Training2");
                CQ_Match_Nonmatch_autoSwitch(n, m, 3,SOneLaserSwitchOrnot, OdorPairSelection, shapingOrTraining, odor_A, 2, 0.0, 1.0, odor_B, 3, 3.0, 4.0, 4, 6.0, 6.5, 10.5, 6, 6, 20, odor_C, odor_D);
                break;
             case 2123:
                   n = getFuncNumber(1, "BGo3 JGo4 Rand");
                   m = getFuncNumber(1, "NL1 O2 D3 W4 I5");
                   SOneLaserReverseOrnot = getFuncNumber(1, "SOneL1 RevOrnot2");
                   CQ_AutoGoNogoTask(n, m, SOneLaserReverseOrnot,odor_A,2,odor_B,3,0.0,0.5,4,2.0,2.5,7.0,6,6,20,99);
             break;
                //////////////HZ functions/////////////
            case 2511:
                HZ_Go_Nogo(0.5, 5.0, 20, 1.0, 10, 10, 0.0);
                break;
            case 2512:
                DualTask(2.0, 15.0, 24.0, 0.5, 10.0, 40.0, 0.0);
                break;
            case 2513:
                HZ_AB_BA_Go_AA_BB_Nogo_laser_ITI(4, 10, 20, 0.5, 50, 5, 1.0, 2, 4);
                break;
            case 2514:
                HZ_AB_BA_Go_AA_BB_Nogo_laser(5, 10, 20, 0.5, 50, 5, 1.0, 1, 4);
                break;
                ////////////////LD functions/////////////
            case 3355:
                pico(100, 900);
                break;
                //zx's functions
            case 4311 ... 4455:
                callFunction(n);
                break;
            case 5555:
                odorDepeltion(120);
                break;
        }
    }
    return 0;
}
